import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {adSystemApiUrl, tpType, tuikeSystemUrl} from '../../../config/api';
import {HttpService} from '../../lib/service/http.service';
import {LocalStorageService} from '../../lib/service/local-storage.service';

@Injectable()
export class PromoteService {

  constructor(private httpService: HttpService,
              private localStorageService: LocalStorageService) {}

  getListData(keyword, search_type, status, page, pageSize) {
    const url = `${adSystemApiUrl}/v2/member/tuike/task/index`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      keyword: keyword || '',
      search_type: search_type,
      status: status,
      page: page,
      pageSize: pageSize
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 商品列表
  getGoodsList(page: number,
               pageSize: number,
               keyword?: string) {
    const _keyword = keyword || '';
    const url = `${adSystemApiUrl}/v1/member/goods/list` +
      `?status=上架&keyword=${_keyword}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  //关键词列表
  getKeywordList(page:number) {
    const url = `${adSystemApiUrl}/v2/member/tuike/task/tuike_task_key`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({page:page,pageSize:999});
    // const url = `${adSystemApiUrl}/v1/admin/tuike/tuike_category/category_list?level=0&parent=0&pageSize=200`;
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 活动列表
  getActivityList( page: number,
                   pageSize: number,
                   activityGoodsType: number,
                   winnersNumber: number,
                   keyWord?: string) {
    const _keyword = keyWord || '';
    const url = `${adSystemApiUrl}/v2/member/activity/task_use_index?page=${page}&pageSize=${pageSize}&activity_goods_type=${activityGoodsType}&winners_number=${winnersNumber}&keyword=${_keyword}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  //获取分类列表
  getCategoryList(level?,parent_id?) {
    const url = `${adSystemApiUrl}/v2/member/tuike/task/tuike_category`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({level:level,parent_id:parent_id});
    // const url = `${adSystemApiUrl}/v1/admin/tuike/tuike_category/category_list?level=0&parent=0&pageSize=200`;
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 添加活动
  addTask(data) {
    const url = `${adSystemApiUrl}/v2/member/tuike/task/add`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 修改任务状态
  changeStatus(id, task_no, status) {
    const url = `${adSystemApiUrl}/v2/member/tuike/task/status`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      id: id,
      task_no: task_no,
      status: status
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取任务详情
  getTaskInfo(id) {
    const url = `${adSystemApiUrl}/v2/member/tuike/task/info`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      id: id
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  //修改任务名称
  editTaskName(data) {
    const url = `${adSystemApiUrl}/v2/member/tuike/task/edit`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 根据积分获取所需等级
  getLevelByPoint(point) {
    const url = `${adSystemApiUrl}/v2/member/tuike/member_level/get_level_by_point`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      point: point
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  getAccountInfo() {
    const url = `${adSystemApiUrl}/v2/member/tuike/account/info`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({});
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取商品或者活动
  getInfo(type, id) {
    const url = `${adSystemApiUrl}/v1/fddata/gd_info/info`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      type: type,
      id: id
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 申请开通推客
  applyTuike() {
    const url = `${adSystemApiUrl}/v2/member/tuike/account/apply`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    console.log(this.localStorageService.getNowApplet().nick_name);
    const body = JSON.stringify({
      // name: name,s
      name:this.localStorageService.getNowApplet().nick_name,
      tp_key: this.localStorageService.getNowApplet().applet_uuid,
      tp_type: tpType
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取小程序详情
  getAppletInfo() {
    const url = `${adSystemApiUrl}/v1/member/applets/applet_detail`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }
}
